//
//  LZClearCacheCell.m
//  百思不得姐
//
//  Created by 郝庆 on 16/5/22.
//  Copyright © 2016年 haoqing. All rights reserved.
//

#import "LZClearCacheCell.h"
#import <SDImageCache.h>
#import <SVProgressHUD.h>

#define LZCustomCacheFile [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"Custom"]

@implementation LZClearCacheCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        // 设置cell的右边指示器
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        // 设置cell默认的文字(如果设置文字之前userInteractionEnabled = NO,那么文字会自动变成浅灰色)
        self.textLabel.text = @"清除缓存(正在计算缓存大小...)";
        
        // 禁止点击
        self.userInteractionEnabled = NO;
        
//        __weak LZClearCacheCell * weakSelf = self;
        __weak typeof(self) weakSelf = self;
        
        // 在子线程计算缓存大小
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            
            
            // 获得缓存文件夹路径
            unsigned long long size = LZCustomCacheFile.lz_fileSize;
            size = [SDImageCache sharedImageCache].getSize;
            
            // 做完一些耗时操作后,判断cell是否销毁,如果cell已经销毁了, 就直接返回
            if (weakSelf == nil) return;
            
            NSString *sizeText = nil;
            if (size >= pow(10, 9)) { // size >= 1GB
                sizeText = [NSString stringWithFormat:@"%.2fGB", size / pow(10, 9)];
            } else if (size >= pow(10, 6)) { // 1GB > size >= 1MB
                sizeText = [NSString stringWithFormat:@"%.2fMB", size / pow(10, 6)];
            } else if (size >= pow(10, 3)) { // 1MB > size >= 1KB
                sizeText = [NSString stringWithFormat:@"%.2fKB", size / pow(10, 3)];
            } else { // 1KB > size
                sizeText = [NSString stringWithFormat:@"%zdB", size];
            }
            
            // 生成文字
            NSString *text = [NSString stringWithFormat:@"清除缓存(%@)", sizeText];
            
            // 回到主线程
            dispatch_async(dispatch_get_main_queue(), ^{
                // 设置文字
                weakSelf.textLabel.text = text;
                
                // 显示右边的箭头
                weakSelf.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                
                // 添加手势监听器
                [weakSelf addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:weakSelf action:@selector(clickClearCache)]];
                
                // 恢复点击事件
                weakSelf.userInteractionEnabled = YES;
            });
            
        });
    }
    return self;
}

/**
 *  清除缓存
 */
- (void)clickClearCache
{
    // 弹出指示器
    [SVProgressHUD showWithStatus:@"正在清除缓存..."];
    // 删除SDWebImage的缓存
    [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
        // 删除自定义的缓存
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            NSFileManager *mgr = [NSFileManager defaultManager];
            [mgr removeItemAtPath:LZCustomCacheFile error:nil];
            [mgr createDirectoryAtPath:LZCustomCacheFile withIntermediateDirectories:YES attributes:nil error:nil];
            
            // 所有的缓存都清除完毕
            dispatch_async(dispatch_get_main_queue(), ^{
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    // 隐藏指示器
                    [SVProgressHUD showSuccessWithStatus:@"清理已完毕"];
                });
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [SVProgressHUD dismiss];
                });
                // 设置文字
                self.textLabel.text = @"清除缓存(0M)";
            });
        });
    }];

}
@end
